package com.yc.cloudvideo.sp_store.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.yc.cloudvideo.entity.Comment;
import com.yc.cloudvideo.sp_store.mapper.CommentMapper;
import com.yc.common.bean.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LYYT
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/cloudvideo/comment")
public class CommentController {

    @Resource
    CommentMapper commentMapper;

    @PostMapping("queryCommentSon")
    String selectCommentSon(@RequestParam("parentid")long parentid){
        final List<Comment> comments = commentMapper.selectCommentSon(parentid);
        return new Gson().toJson(comments);
    }

    @PostMapping("talk")
    Result talk(
            long uid,
            @RequestParam("body")String body,@RequestParam("cid")long cid){
        Comment comment=new Comment();
        comment.setCid(cid);
        comment.setUid(uid);
        comment.setTalkTime(LocalDateTime.now());
        comment.setBody(body);
        if(commentMapper.insert(comment)==1){
            return new Result(1,"评论成功",null);
        }else {
            return new Result(0,"评论失败",null);
        }
    }

    @PostMapping("talkto")
    Result talkto(
            long uid,
            @RequestParam("body")String body,@RequestParam("cid")long cid,
            @RequestParam("parentid")long parentid,@RequestParam("brotherid")long brotherid){
        Comment comment=new Comment();
        comment.setCid(cid);
        comment.setUid(uid);
        comment.setTalkTime(LocalDateTime.now());
        comment.setBody(body);
        comment.setParentid(parentid);
        if(brotherid!=-1){
            comment.setBrotherid(brotherid);
        }
        if(commentMapper.insert(comment)==1){
            return new Result(1,"回复成功",null);
        }else {
            return new Result(0,"回复失败",null);
        }
    }

    @PostMapping("queryCommentByDesc")
    String queryCommentByDesc(@RequestParam("cid")long cid,@RequestParam("size")int size
            ,@RequestParam(value = "page",defaultValue = "1")int page){
        final List<Comment> comments = commentMapper.selectCommentByCidDesc(cid,(page-1)*size,size);
        int total=commentMapper.selectCommentCount(cid);
        long[] pids=new long[comments.size()];
        long[] reply=new long[comments.size()];
        for (int i = 0; i < comments.size(); i++) {
            pids[i]=comments.get(i).getPid();
            reply[i]=commentMapper.selectCommentReply(pids[i]);
        }
        HashMap<String,Object> map=new HashMap<>();
        map.put("reply",reply);
        map.put("comments",comments);
        map.put("total",total);
        return new Gson().toJson(map);
    }

    @PostMapping("queryCommentByLikenum")
    String queryCommentByLikenum(@RequestParam("cid")long cid,@RequestParam("size")int size
            ,@RequestParam(value = "page",defaultValue = "1")int page){
        final List<Comment> comments = commentMapper.selectCommentByLikenum(cid,(page-1)*size,size);
        int total=commentMapper.selectCommentCount(cid);
        long[] pids=new long[comments.size()];
        long[] reply=new long[comments.size()];
        for (int i = 0; i < comments.size(); i++) {
            pids[i]=comments.get(i).getPid();
            reply[i]=commentMapper.selectCommentReply(pids[i]);
        }
        HashMap<String,Object> map=new HashMap<>();
        map.put("reply",reply);
        map.put("comments",comments);
        map.put("total",total);
        return new Gson().toJson(map);
    }
}

